package edu.uta.cfl.engine;

import java.awt.Color;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Vector;
import java.util.regex.Pattern;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

import edu.uta.cfl.data.Parameter;
import edu.uta.cfl.data.SUT;
import edu.uta.cfl.engine.SUTInfoReader;

/**
 * This class will read the input file
 * load the contents of input to the GUI
 * this is similar to SUTInfoReader.java
 * @author jagan
 *
 */
public class DisplayInputFile {
	private String file;
	private SUT sut;
	private ArrayList<String> parameters_name = new ArrayList<String>();
	private ArrayList<String> parameters_value = new ArrayList<String>();
	private ArrayList<String> constraintsValue = new ArrayList<String>();
	private ArrayList<String> testResult_value = new ArrayList<String>();
	private ArrayList<String> testResults = new ArrayList<String>();
	private ArrayList<String> initialTestSet = new ArrayList<String>();
	private Vector<Object> row1 = new Vector<Object>();
	
	public ArrayList<String> getInitialTestSet() {
		return initialTestSet;
	}

	public void setInitialTestSet(ArrayList<String> initialTestSet) {
		this.initialTestSet = initialTestSet;
	}

	public ArrayList<String> getTestSetColumnName() {
		return testSetColumnName;
	}

	public void setTestSetColumnName(ArrayList<String> testSetColumnName) {
		this.testSetColumnName = testSetColumnName;
	}


	private ArrayList<String> testSetColumnName = new ArrayList<String>();

	private ArrayList<Parameter> parameters_initialForm = new ArrayList<Parameter>();


	public DisplayInputFile(String inputFile) {
		super();
		this.file = inputFile;
		readSUT(file);
	}

	private void readSUT(String file) {
		sut = new SUT();
		sut = (new SUTInfoReader(file)).getSUT();
	}

	public ArrayList<String> getParameters_name() {
		return parameters_name;
	}

	public void setParameters_name(ArrayList<String> parameters_name) {
		this.parameters_name = parameters_name;
	}

	public ArrayList<String> getParameters_value() {
		return parameters_value;
	}

	public void setParameters_value(ArrayList<String> parameters_value) {
		this.parameters_value = parameters_value;
	}

	public ArrayList<String> getConstraintsValue() {
		return constraintsValue;
	}

	public void setConstraintsValue(ArrayList<String> constraintsValue) {
		this.constraintsValue = constraintsValue;
	}

	public ArrayList<String> getTestResult_value() {
		return testResult_value;
	}

	public void setTestResult_value(ArrayList<String> testResult_value) {
		this.testResult_value = testResult_value;
	}

	public ArrayList<String> getTestResults() {
		return testResults;
	}

	public void setTestResults(ArrayList<String> testResults) {
		this.testResults = testResults;
	}

	public void readInputFile()
	{
		parameters_initialForm = sut.getParameters();
		for (Parameter p : parameters_initialForm) {
			parameters_name.add(p.getName());
			parameters_value.add(p.getValues().toString());
		}

		for(int count= 0;count<sut.getConstraints().size();count++)
		{
			constraintsValue.add(sut.getConstraints().get(count));
		}

		generateTestSetColumnName();
		readInitialTestSet();

	}

	public void generateTestSetColumnName() {
		int index = 1;
		testSetColumnName.add("Row Index");
		for (Parameter prm : parameters_initialForm)
		{
			testSetColumnName.add(prm.getName());
			index++;
		}
		testSetColumnName.add("Result");
	}


	public void readInitialTestSet() {
		/**
		 * below logic will read do the following
		 * #1 - read all the test result and store in testResult_value
		 * #2 - based on "1" or "0", final result will be stored in the ArrayList - testResults
		 */
		int testResultSize = sut.getTs().getResult().size();
		for (int count = 0; count < testResultSize; count++) {
			testResult_value.add(sut.getTs().getResult().get(count).toString());
		}

		for (int count = 0; count < testResult_value.size(); count++) {
			String pass = "Pass";
			String fail = "fail";
			System.out.println("Count :" + count + "   Value :"
					+ testResult_value.get(count).toString());

			if (testResult_value.get(count).toString().equalsIgnoreCase("1")) {
				testResults.add(count, pass);

			} else if (testResult_value.get(count).toString()
					.equalsIgnoreCase("0")) {
				testResults.add(count, fail);

			}
		} // end of logic for reading test results.
		
		int testSetSize = sut.getTs().getMatrix().size();
		String arrayString[] = new String[testSetSize];
		for (int i = 0; i < testSetSize; i++) {
			arrayString[i] = Arrays.toString(sut.getTs().getMatrix().get(i));
		}
		int rowCount = 0;
		for (String k : arrayString) {
			k = k.replace("[", "");
			k = k.replace("]", "");
			Pattern paramPattern = Pattern.compile(",");
			String[] tokens = paramPattern.split(k);
			for (int i = 0; i < tokens.length; i++) {
				initialTestSet.add(tokens[i]);
			}
			initialTestSet.add(testResults.get(rowCount));
			
			
			int count = 1;
			
			for (int i = 0; i < initialTestSet.size(); i++) {
				// row1.add(tokens[i]);
				row1.add(initialTestSet.get(i));
				
				count++;
			}
			rowCount++;
		}
	}
}